package com.cscp.javastudy.juc.pool;

import java.util.concurrent.*;

public class ThreadPoolDemo {

    public static void main(String[] args) {
        //自定义线程池
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());//线程已经用完并且队列满了后采取的策略
        /*
         *四大拒绝策略
         * new ThreadPoolExecutor.AbortPolicy()//队列满了直接拒绝任务并抛出异常
         * new ThreadPoolExecutor.CallerRunsPolicy()//从哪里来到哪里去，丢给调用者线程去处理
         * new ThreadPoolExecutor.DiscardPolicy()//队列满了丢掉任何但不会抛出异常
         * new ThreadPoolExecutor.DiscardOldestPolicy()//队列满了后尝试和最早的去竞争，不会抛出异常
         */

        try {
            for (int i = 1; i <= 9; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+ " OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}
